/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package semanticrawler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JTextArea;
import utfpr.cp.crawler.ControllerCrawler;
import utfpr.cp.crawler.CrawlerStats;
import utfpr.cp.model.DAO;
import utfpr.cp.model.HTMLTagDAO;
import utfpr.cp.model.ResourceDataDAO;
import utfpr.cp.model.URLDataDAO;
import utfpr.cp.model.URLTarget;
import utfpr.cp.model.URLTargetDAO;


/**
 *
 * @author COINF-PROFESSOR-07
 */
public class RunCrawler {

    /**
     * @param args the command line arguments
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws Exception {
        Main.main(args);
    }
    
    public static int NUMERO_THREADS = 8;
    protected static ControllerCrawler getNovoServidor(boolean mode) {
        ControllerCrawler control = new ControllerCrawler();
        control.loadProxyConfig("C:/web/proxyutf.conf");
        control.setRestrictMode(mode);
        control.setNumberOfCrawlers(NUMERO_THREADS);
        return control;
    }
    
    public static ControllerCrawler ligarServidorAutomatizado(boolean mode) throws Exception {
        ControllerCrawler control = getNovoServidor(mode);
        List<URLTarget> lista = URLTargetDAO.factory().getAll();
        for (URLTarget url : lista) {
            control.addSite(url.getUrl());
        }
        control.run();
        return control;
    }
    
    public static void ligarServidorPorEtapa(final boolean mode, final JTextArea console) throws Exception {
       Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                ControllerCrawler control = null;
                ArrayList<URLTarget> targets = null;
                EntityManager em = DAO.getEntity();
                DateFormat dateFormat = new SimpleDateFormat("dd/MM HH:mm:ss");
                int time = 15;
           
                while (true) {
                    if (control == null) {
                        time = 60;
                        targets = new ArrayList<>();
                        URLTarget target = null;
                        control = getNovoServidor(mode);
                        URLTargetDAO dao = new URLTargetDAO(em);
                        int total = NUMERO_THREADS / 8;
                        for (int i = 0; i < total; i++) {
                            try {
                                target = dao.getNextURL();
                            }
                            catch (Exception e) {
                                e.printStackTrace(); 
                                em = DAO.getEntity();
                            }

                            if (target != null) {
                                control.addSite(target.getUrl());
                                if (!em.contains(target)) {
                                    target = em.merge(target);
                                }
                                em.getTransaction().begin();
                                target.setStarted(true);
                                em.getTransaction().commit();
                                targets.add(target);
                            }
                        }
                        try {
                            control.run();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        continue;
                    }
                    else if (control.isFinished()) {
                        for (URLTarget target : targets) {
                            if (target != null) {
                                if (!em.contains(target)) {
                                    target = em.merge(target);
                                }
                                em.getTransaction().begin();
                                CrawlerStats resumo = control.getResumoAtual();
                                if (resumo.getTotalSavedPages() < 10 ||
                                    (resumo.getTotalErrors() + resumo.getTotalDatabaseErrors() > resumo.getTotalSavedPages())
                                ) {
                                    target.setError(true);
                                }
                                else {
                                    target.setFinished(true);
                                }
                                em.getTransaction().commit();
                            }
                        }
                        control = null;
                        time = 1;
                    }
                    else {
                        StringBuilder sb = new StringBuilder();
                        
                        sb.append("Site: ");
                        for (URLTarget target : targets) {
                            sb.append(" - ").append(target.getUrl()).append(" - ");
                        }
                        Date date = new Date();
                        sb.append("\n").append(dateFormat.format(date));
                        CrawlerStats resumo = control.getResumoAtual();
                        sb.append(resumo);
                        for (CrawlerStats st : control.getStatisticsList()) {
                            sb.append("\n").append(st);
                        }
                        if (
                            resumo.getTotalProcessedURL() > 350000 ||
                            resumo.getTotalSavedPages() > 30000 ||
                            resumo.getLastTime() > 60000   //foi salvo pela ultima vez a 1 min.
                        ) {
                            sb.append("\nFinish...");
                            control.shutdown();
                        }
                        console.setText(sb.toString());
                        time = 15;
                    }
                    
                    try {
                        Thread.sleep(1000 * time);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(myRunnable);
        thread.start();
    }
    
    public static ControllerCrawler ligarServidorManual(boolean mode, String[] sites) throws Exception {
        ControllerCrawler control = getNovoServidor(mode);
        for (String url : sites) {
            control.addSite(url);
        }
        control.run();
        return control;
    }
    
    public static ControllerCrawler ligarServidorTeste(boolean mode) throws Exception {
        ControllerCrawler control = getNovoServidor(mode);
        control.addSite("http://www.utfpr.edu.br");
        control.run();         
        return control;
    }
    
    public static HashMap<String, Long> getInfoGeral() {
        HashMap<String, Long> dados = new HashMap<>();
        EntityManager em = DAO.getEntity();
        dados.put("tags", new HTMLTagDAO(em).getTotalTags());
        dados.put("attr", new HTMLTagDAO(em).getTotalAttributes());
        dados.put("pagina", new ResourceDataDAO(em).getTotalPages());
        dados.put("header", new ResourceDataDAO(em).getTotalHeaders());
        dados.put("parser", new ResourceDataDAO(em).getTotalResoursesToParser());
        dados.put("url", new URLDataDAO(em).getTotalURL() - dados.get("pagina"));
        dados.put("sites", new URLTargetDAO(em).getTotalSite());
        return dados;
    } 
}
